package org.zhang.shortlink.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.zhang.shortlink.admin.common.convention.result.Result;
import org.zhang.shortlink.admin.common.convention.result.Results;
import org.zhang.shortlink.admin.dto.request.UserLoginReqDTO;
import org.zhang.shortlink.admin.dto.request.UserRegisterReqDTO;
import org.zhang.shortlink.admin.dto.request.UserUpdateReqDTO;
import org.zhang.shortlink.admin.dto.response.ActualUserRespDto;
import org.zhang.shortlink.admin.dto.response.UserLoginRespDTO;
import org.zhang.shortlink.admin.dto.response.UserRespDto;
import org.zhang.shortlink.admin.service.UserService;

/**
 * 用户管理层
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/short-link/admin/v1")
public class UserController {

    private final UserService userService;

    /**
     * 根据用户名查询信息
     */
    @GetMapping("/user/{username}")
    public Result<UserRespDto> getUserByUsername(@PathVariable("username")String username) {
            return Results.success(userService.getUserByUsername(username));
    }

    /**
     * 根据用户名查询用户真实信息
     */
    @GetMapping("/actual/user/{username}")
    public Result<ActualUserRespDto> getUserByUsername1(@PathVariable("username")String username) {
        return Results.success(BeanUtil.toBean(userService.getUserByUsername(username), ActualUserRespDto.class));
    }

    /**
     * 查询用户名是否存在
     */
    @GetMapping("/user/has-username")
    public Result<Boolean> hasUsername(@RequestParam("username")String username){
        return Results.success(userService.hasUserName(username));
    }

    /**
     * 用户注册
     * @param userRegisterReqDTO
     * @return
     */
    @PostMapping("/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO userRegisterReqDTO){
        userService.register(userRegisterReqDTO);
        return Results.success();
    }

    /**
     * 修改用户
     * @param userUpdateReqDTO
     * @return
     */
    @PutMapping("/user")
    public Result<Void> update(@RequestBody UserUpdateReqDTO userUpdateReqDTO){
        userService.update(userUpdateReqDTO);
        return Results.success();
    }

    /**
     * 用户登录
     * @param userLoginReqDTO
     * @return
     */
    @PostMapping("/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO userLoginReqDTO){
        return Results.success(userService.login(userLoginReqDTO));
    }

    /**
     * 检查用户是否登录
     */
    @GetMapping("/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam("username") String username, @RequestParam("token") String token) {
        return Results.success(userService.checkLogin(username, token));
    }

    /**
     * 用户退出登录
     */
    @DeleteMapping("/user/logout")
    public Result<Void> logout(@RequestParam("username") String username,@RequestParam("token") String token){
        userService.logout(username,token);
        return Results.success();
    }
}
